/*!
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *    Author: flareguner
 *    E-Mail: flareguner@gmail.com
 *    Years: 2010-2011
 */


#include "languagesconfig.h"
#include "translate.h"

#include <QtGui/QTableWidget>
#include <QtGui/QPushButton>
#include <QtGui/QHBoxLayout>
#include <QtGui/QVBoxLayout>
#include <QtGui/QCheckBox>
#include <QtGui/QListWidgetItem>
#include <QtCore/QSettings>
#include <QtCore/QEvent>
#include "src/platform.h"




LanguagesConfig::LanguagesConfig(QWidget *parent) :
    QWidget(parent)
{
    createUi();    changeEvent(new QEvent(QEvent::LanguageChange));

    fillTable();
    m_settings = new QSettings(Platform::getApplicationName(),
                               Platform::getOrganizationName());


    m_settings->beginGroup("Languages");

    connect(selectAllButton, SIGNAL(clicked()), this, SLOT(selectAll()));
    connect(deselectAllButton, SIGNAL(clicked()),this, SLOT(deselectAll()));

}

LanguagesConfig::~LanguagesConfig() {
    delete languagesTable;
    delete selectAllButton;
    delete deselectAllButton;
    delete buttonsLayout;
    delete mainLayout;
}

QStringList LanguagesConfig::getEnabledLanguagesCodes() const {
    QStringList enabled_list;

    for(int i = 0; i < languagesTable->rowCount(); i++) {
        QWidget *cw = languagesTable->cellWidget(i, 1);
        QCheckBox *cb = qobject_cast<QCheckBox *>(cw);
        if(cb->isChecked()) {
            enabled_list << LanguagesFactory::shortList().at(i);
        }
    }
    return enabled_list;
}

void LanguagesConfig::save() {
    if(this->getEnabledLanguagesCodes().isEmpty()) {
        const QStringList lst = LanguagesFactory::shortList();
        for(int i = 0; i < lst.size(); i++) {
            if(lst.at(i) == "en" ) {
             QWidget *cw = languagesTable->cellWidget(i, 1);
            QCheckBox *cb = qobject_cast<QCheckBox *>(cw);
            cb->setChecked(1);}

        }
    }


    m_settings->setValue("enabled", getEnabledLanguagesCodes());


}
void LanguagesConfig::read() {
    const QStringList enabled = m_settings->value("enabled", LanguagesFactory::shortList()).toStringList();

    for(int i = 0; i < languagesTable->rowCount(); i++) {
        const QString cur = languagesTable->item(i, 0)->text();
        for(int j = 0; j < enabled.size(); j++) {
            if(enabled.at(j) == cur) {
                QWidget *cw = languagesTable->cellWidget(i, 1);
                QCheckBox *cb = qobject_cast<QCheckBox *>(cw);
                cb->setChecked(1);
            }
        }

    }

}

void LanguagesConfig::truncate() {
    this->selectAll();
}

void LanguagesConfig::changeEvent(QEvent *e) {
    if(e->type() == QEvent::LanguageChange) {
        selectAllButton->setText(QPushButton::tr("Select All"));
        deselectAllButton->setText(QPushButton::tr("Deselect All"));
        languagesTable->setHorizontalHeaderLabels(QStringList()
                                                  << QTableWidget::tr("Language")
                                                  << QTableWidget::tr("Enabled"));


    }
}

void LanguagesConfig::selectAll() {
    for(int i = 0; i < languagesTable->rowCount(); i++) {
        QWidget *cw = languagesTable->cellWidget(i, 1);
        QCheckBox *cb = qobject_cast<QCheckBox *>(cw);
        cb->setChecked(true);

    }
}
void LanguagesConfig::deselectAll() {
    for(int i = 0; i < languagesTable->rowCount(); i++) {
        QWidget *cw = languagesTable->cellWidget(i, 1);
        QCheckBox *cb = qobject_cast<QCheckBox *>(cw);
        cb->setChecked(false);
    }
}

void LanguagesConfig::createUi() {
    languagesTable = new QTableWidget(this);
    languagesTable->setColumnCount(2);
    languagesTable->setHorizontalHeaderLabels(QStringList() << "Language" <<"Enabled");
    languagesTable->setVerticalHeaderLabels(QStringList());

    selectAllButton = new QPushButton(this);
    deselectAllButton = new QPushButton(this);

    buttonsLayout = new QHBoxLayout;
    mainLayout = new QVBoxLayout;

    buttonsLayout->addWidget(selectAllButton);
    buttonsLayout->addWidget(deselectAllButton);

    mainLayout->addLayout(buttonsLayout);
    mainLayout->addWidget(languagesTable);

    this->setLayout(mainLayout);
}

void LanguagesConfig::fillTable() {
//    languagesTable->clear();

    const QStringList codes = LanguagesFactory::shortList(),
            names = LanguagesFactory::longList();

    for(int i = 0; i < codes.size(); i++) {
        languagesTable->setRowCount(languagesTable->rowCount() + 1);


        QTableWidgetItem *item = new QTableWidgetItem;
        item->setText(names.at(i));
        item->setIcon(Icons::flag(codes.at(i)));
        languagesTable->setItem(i,0, item);
        languagesTable->setCellWidget(i,1, new QCheckBox(languagesTable));
    }
}
